package com.gaga.metadata.entity.database;


import com.gaga.metadata.common.assertion.Asserts;
import com.gaga.metadata.common.enums.TableUpdateType;
import com.gaga.metadata.entity.base.BasePage;
import com.gaga.metadata.entity.base.BaseTable;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * Table
 */
@Getter
@Setter
@Accessors(chain = true)
public class DataBaseTable extends BaseTable implements Serializable, Comparable<DataBaseTable> {

    private static final long serialVersionUID = 4209205512472367171L;


    private TableUpdateType tableUpdateType;

    /**
     * 表类型
     */
    private String type;
    /**
     * 表引擎
     */
    private String engine;
    /**
     * 表配置
     */
    private String tableOptions;
    /**
     * 表记录数
     */
    private Long rows;
    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 列信息
     */
    private List<DataBaseColumn> columns;


    /**
     * 分页信息
     */
    private BasePage page;

    /**
     * 过滤器
     */
    @Deprecated
    private List<DataBaseFilter> filters;

    /**
     * groupby
     */
    private List<DataBaseGroupBy> groupBys;

    /**
     * groupby
     */
    private List<DataBaseOrderBy> orderBys;

    /**
     * whereCondition
     */
    private DataBaseWhere whereCondition;

    /**
     * join
     */
    private List<BaseDataJoin> joins;


    /**
     * 索引
     */
    private List<DataBaseIndex> indexs;

    public DataBaseTable() {
    }


    public DataBaseTable(String tableName, String schema, List<DataBaseColumn> columns) {
        this.tableName = tableName;
        this.schema = schema;
        this.columns = columns;
    }

    public String getSchemaTableName() {
        return Asserts.isNullString(schema) ? tableName : schema + "." + tableName;
    }

    public String getSchemaTableNameWithUnderline() {
        return Asserts.isNullString(schema) ? tableName : schema + "_" + tableName;
    }

    @Override
    public int compareTo(DataBaseTable o) {
        return this.tableName.compareTo(o.getTableName());
    }

    public static DataBaseTable build(String name) {
        return new DataBaseTable(name, null, null);
    }

    public static DataBaseTable build(String name, String schema) {
        return new DataBaseTable(name, schema, null);
    }

    public static DataBaseTable build(String name, String schema, List<DataBaseColumn> columns) {
        return new DataBaseTable(name, schema, columns);
    }

}
